package com.soul.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.soul.common.bean.vo.FileVo;
import com.soul.common.bean.vo.UserVo;
import com.soul.common.enums.BizCacheKeyEnum;
import com.soul.common.feign.CommDocService;
import com.soul.common.feign.CommSysService;
import com.soul.common.service.CacheService;
import com.soul.common.utils.FormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Shiws
 * @version 1.0
 * @date 2023/11/21 10:38
 */
@Service
public class CacheServiceImpl implements CacheService {

    @Autowired(required = false)
    private CommSysService commSysService;
    @Autowired(required = false)
    private CommDocService commDocService;

    @Override
    public <T> List<T> getGlobalObjectList(Class<T> zClass, Map<String, Set<String>> keyMap){
        List<T> list = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(keyMap)){
            keyMap.forEach((k,v)->{
                if (BizCacheKeyEnum.USER.getCode().equals(k)){
                    // 查询用户
                    if (ObjectUtil.isNotEmpty(v)){
                        List<UserVo> userVos = commSysService.queryUserVoByIds(v.stream().map(FormatUtil::toLong).collect(Collectors.toList()));
                        List<T> users = JSONUtil.toList(JSONUtil.toJsonStr(userVos), zClass);
                        list.addAll(users);
                    }
                }
                if (BizCacheKeyEnum.FILE.getCode().equals(k)){
                    // 查询用户
                    if (ObjectUtil.isNotEmpty(v)){
                        List<FileVo> fileVos = commDocService.queryFileVoByIds(v.stream().map(FormatUtil::toLong).collect(Collectors.toList()));
                        List<T> users = JSONUtil.toList(JSONUtil.toJsonStr(fileVos), zClass);
                        list.addAll(users);
                    }
                }
            });
        }
        return list;
    }
}
